<?php
 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";

 require "../misc/class_tag.php";
 require "../misc/tool_color.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../maths/tool_maths.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";
 require "../pdo/class_sql_document.php";

$file = 'dotplot_alinea_fr::fi.png';
$image=imagecreatefrompng($file);

$filtre = array(
  array(1.0, 0.0, 0.0),
  array(0.0, 0.0, 0.0),
  array(0.0, 0.0, 1.0)
);  

$sum = 0;
foreach($filtre as $l){
  foreach($l as $c){
    $sum += $c;
  }
}
imageconvolution($image, $filtre, $sum, 0);
imagepng($image, "conv_$file");
die();

function complete_mt($_mt_vect,$_data){
  foreach($_data as $d){
    list($id_bloc,$nb_occ)=explode(":",$d);
    $_mt_vect[$id_bloc]=$nb_occ;
  }
  return $_mt_vect;
}

function complete_mt2($_mt_vect,$_data){
  foreach($_data as $id_grain=>$d){
    $_mt_vect[$id_grain]=$d;
  }
  return $_mt_vect;
}

function convert_dist2gray($_dist){
  return 255 - round(255*$_dist);
}

function dictionnaire_color(&$_image,&$_dict,$_dist){
  $color = convert_dist2gray($_dist); 
  if(!isset($_dict[$color]))
    $_dict[$color] = imagecolorallocate($_image,$color,$color,$color);
  return $_dict[$color];
}

$exp=new sql_experience("align_ecr_word");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$all_lg = $array_obj_sql['sql_document']->reader_all_lg();

$corpus_multidoc = array();
foreach($all_lg as $l1){
  $lg = $l1['lg'];
  $array_id_document = $array_obj_sql['sql_document']->reader_lg2array_id_document($lg);
  $cpt = 0;
  foreach($array_id_document as $l2){
    $id = $l2['id_document'];
    if(isset($corpus_multidoc[$cpt])) $corpus_multidoc[$cpt][$lg] = $id;
    else $corpus_multidoc[$cpt] = array($lg => $id);
    ++$cpt;
  }
}

$multidoc = $corpus_multidoc[0];

$lg_src = "fr";
$lg_tgt = "es";

$doc_src = $multidoc[$lg_src];
$doc_tgt = $multidoc[$lg_tgt];

$all_graphie_src = $array_obj_sql['sql_graphie']->reader_id_document2graphie($doc_src);
$all_graphie_tgt = $array_obj_sql['sql_graphie']->reader_id_document2graphie($doc_tgt);

$array_corres_src = $array_obj_sql["sql_rstr"]->reader_all2relative_id_bloc($lg_src);
$array_corres_tgt = $array_obj_sql["sql_rstr"]->reader_all2relative_id_bloc($lg_tgt);


$mt_vector = array_fill(1,count(reset($array_corres_src)),0);
$base_vector = array_keys($mt_vector);
$count_vector = count($mt_vector);

$res_all_count_bloc = array();

$image = imagecreate(count($all_graphie_src),count($all_graphie_tgt));
$dico_color = array();
dictionnaire_color($image,$dico_color,0);
foreach($all_graphie_src as $i=>$g_fr){
  $graphie_fr = $g_fr['graphie'];
  $rep_src = $array_obj_sql["sql_graphie"]->reader_lg_graphie2repartition_bloc($lg_src,$graphie_fr,$array_corres_src);
  $rep_src[$graphie_fr] = complete_mt2($mt_vector,$rep_src[$graphie_fr]);
  foreach($all_graphie_tgt as $j=>$g_en){
    $graphie_en = $g_en['graphie'];
    $rep_tgt = $array_obj_sql["sql_graphie"]->reader_lg_graphie2repartition_bloc($lg_tgt,$graphie_en,$array_corres_tgt);
    $rep_tgt[$graphie_en] = complete_mt2($mt_vector,$rep_tgt[$graphie_en]);
    $angle = 1-(tool_maths::get_angle($rep_src[$graphie_fr],$rep_tgt[$graphie_en],$base_vector,$count_vector));
//    $jac = tool_maths::get_jaccard($rep_src[$graphie_fr],$rep_tgt[$graphie_en],$base_vector,$count_vector);
//    $im = tool_maths::get_IM($rep_src[$graphie_fr],$rep_tgt[$graphie_en],$base_vector,$count_vector);
    $color = dictionnaire_color($image,$dico_color,$angle);
    imageSetPixel($image,$i,$j,$color);
//    print $graphie_fr."::".$graphie_en." ".$angle." ".$jac." ".$im."\n";
    echo "$i/$j\r";
  }
}

imagepng($image, "dotplot_alinea_$lg_src::$lg_tgt.png");

